import openpyxl

workbook = openpyxl.load_workbook('./decoding.xlsx')
sheet = workbook.active

file = open("./decoding.v", "w", encoding='utf-8')

print("test:"+sheet.cell(row=1, column=2).value)

row = 48 # Number of instruction
column = 14 # Number of operations
extra = 1 # Additional data quantity

file.write("\tcasex(decoding_in_code)\n")

for i in range(row):
    
    #print(str(i+2)+" "+str(column+3) +"\n")
    code = str(sheet.cell(row=i+2, column=column+2+extra).value)
    code_group = str.splitlines(code)
    file.write("\t\t")
    if(code != "default"):
        file.write("32'b")
    for g in code_group:
        if(g == "rd" or g == "rs1" or g == "rs2" or g == "imm[4:1|11]" or g == "imm[4:0]" or g == "zimm" or g == "shamt"):
            file.write("?????")
        elif(g == "imm[11:0]" or g == "csr"):
            file.write("????????????")
        elif(g == "imm[31:12]"or g == "imm[20|10:1|11|19:12]"):
            file.write("????????????????????")
        elif(g == "imm[12|10:5]" or g == "imm[11:5]"):
            file.write("???????")
        elif(g == "pred" or g == "succ"):
            file.write("????")
        else:
            file.write(g)
    
    
    file.write(" : begin //"+(sheet.cell(row=i+2, column=1).value) +"\n")
    for j in range(column):
        #print(str(i)+" "+str(j) +"\n")

        # product op
        file.write("\t\t\t" + str(sheet.cell(row=1, column=j+2).value) + " <= " )
        if(str(sheet.cell(row=1, column=j+2).value) == "op_code_alu"):
            file.write("5'h")
        elif(str(sheet.cell(row=1, column=j+2).value) ==  "op_data_mux2" or str(sheet.cell(row=1, column=j+2).value) == "op_regs_save_mux" or str(sheet.cell(row=1, column=j+2).value) == "op_WHB"):
            file.write("2'h")
        elif(str(sheet.cell(row=1, column=j+2).value) ==  "op_mem_byteable"):
            file.write("4'h")
        elif(str(sheet.cell(row=1, column=j+2).value) ==  "op_csr_add"):
            file.write("3'h")
        else:
            file.write("1'b")
        file.write(str(sheet.cell(row=i+2, column=j+2).value) + ";\n")

    file.write("\t\tend\n\n")
    
file.write("\tendcase\n")


file.close()
